package com.koron.css2.serviceManage.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 服务日志切面
 * 
 * @author Gaoyuan
 * @date 2018年7月12日
 *
 */
@Aspect
@Component
public class ServiceLogAspect {
	private static final Logger logger = LogManager.getLogger(ServiceLogAspect.class);
	
	@Pointcut("execution(* com.koron.serviceManage.servlet..*(..))")
	private void point() {
	}

	@Around("point()")
	private Object doAround(ProceedingJoinPoint joinPoint) {
		Object retObj = null;
		try {
			retObj = joinPoint.proceed();
		} catch (Throwable e) {
			logger.error("Exception:", e);
		}
		
	//	recordLog(joinPoint, retObj);

		return retObj;
	}

/*	*//**
	 * 异步记录日志
	 * @param joinPoint
	 * @param retObj
	 *//*
	private void recordLog(ProceedingJoinPoint joinPoint, Object retObj) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();

		String ua = request.getHeader("User-Agent");
		UserAgent userAgent = UserAgent.parseUserAgentString(ua);
		String browser = userAgent.getBrowser().getName();

		String remoteAddr = request.getRemoteAddr();
		String serviceCode = joinPoint.getSignature().getName();
		// caller和createName暂时不使用，保留
		String caller = "系统";
		String createName = "system";

		TServiceLog log = new TServiceLog();
		log.setCaller(caller);
		log.setIpAddr(remoteAddr);
		log.setServiceCode(serviceCode);
		log.setCreateName(createName);

		String descPattern = "调用方:{0},创建者:{1},客户端IP地址:{2},服务标识:{3},浏览器:{4},输入参数:{5},返回结果:{6}";
		String desc = MessageFormat.format(descPattern, caller,createName,remoteAddr,serviceCode,browser,JsonUtils.objectToJson(request.getParameterMap()),JsonUtils.objectToJson(retObj));

		threadPoolTaskExecutor.execute(new ServiceLogThread(log, logService, desc));
	}*/

}
